using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListaFacil
{
    public partial class frmCadastroTelefoneUtil : Form
    {

        private DataTable dtTelefoneUtil;
        private DataTable dtSecao;
        private BindingSource bsCidade = new BindingSource();
        private BindingSource bsSecao = new BindingSource();
        private BindingSource bsTelefoneUtil = new BindingSource();
        private bool editMode = false;

        public frmCadastroTelefoneUtil()
        {
            InitializeComponent();
            preencheGridTelefonesUteis();
        }

        private void preencheGridTelefonesUteis()
        {
            this.dtTelefoneUtil = Program.SLF.consultarTelefonesUteisCadastrados();
            this.dtSecao = Program.SLF.consultarSecoesTelUtilCadastradas();
            this.bsSecao.DataSource = this.dtSecao;
            this.bsTelefoneUtil.DataSource = this.dtTelefoneUtil;
            this.bindingNavigator1.BindingSource = this.bsTelefoneUtil;
            this.dataGridView1.DataSource = this.bsTelefoneUtil;

        }

        private void HabilitaBotoes(bool flagHabilita)
        {
            bindingNavigatorAddNewItem.Enabled = flagHabilita;
            bindingNavigatorDeleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            edittoolStripButton.Enabled = flagHabilita;
            saveToolStripButton.Enabled = !flagHabilita;
            canceltoolStripButton.Enabled = !flagHabilita;
            txtTelefone.Enabled = !flagHabilita;
            txtTitular.Enabled = !flagHabilita;
            cboSecao.Enabled = !flagHabilita;
            cboCidade.Enabled = !flagHabilita;
            dataGridView1.Enabled = flagHabilita;
        }
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                txtTelefone.Text = dataGridView1.CurrentRow.Cells["numero"].Value.ToString();
                txtTitular.Text=dataGridView1.CurrentRow.Cells["nome"].Value.ToString();
                cboSecao.SelectedValue=Convert.ToInt32( dataGridView1.CurrentRow.Cells["id_secao"].Value);
                cboCidade.SelectedValue=Convert.ToInt32( dataGridView1.CurrentRow.Cells["id_cidade"].Value);
                this.Navegacao(this, e);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void EdittoolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void canceltoolStripButton_Click(object sender, EventArgs e)
        {
            this.preencheGridTelefonesUteis();
            this.HabilitaBotoes(true);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            int numeroregistro = 0;
            int linha = 0;
            string retorno = "";

            linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
            numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells["id_tel_util"].Value);

            retorno = Program.SLF.removerTelefoneUtil(numeroregistro);

            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);

            this.preencheGridTelefonesUteis();
            this.HabilitaBotoes(true);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string retorno = "";
            int numeroregistro = 0;
            int linha = 0;

            if (this.editMode == false)
            {
                retorno = Program.SLF.inserirTelefoneUtil(txtTitular.Text.ToString().Replace("'","''"), Convert.ToInt32(txtTelefone.Text.Replace("-", "")),((int) cboCidade.SelectedValue), ((int)cboSecao.SelectedValue));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
                numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells["id_tel_util"].Value);
                retorno = Program.SLF.updateTelefoneUtil(numeroregistro, txtTitular.Text, Convert.ToInt32(txtTelefone.Text.Replace("-","")), Convert.ToInt32(cboCidade.SelectedValue.ToString()), Convert.ToInt32(cboSecao.SelectedValue.ToString()));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.preencheGridTelefonesUteis();
            this.HabilitaBotoes(true);
        }


        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void Edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }

        private void frmCadastroTelefoneUtil_Load(object sender, EventArgs e)
        {
            this.bsSecao.DataSource = Program.SLF.consultarSecoesTelUtilCadastradas();
            this.cboSecao.DataSource = bsSecao.DataSource;
            this.cboSecao.DisplayMember = "nome";
            this.cboSecao.ValueMember = "id_secao";

            cboCidade.DataSource = Program.SLF.consultarCidadesCadastradas();
            cboCidade.ValueMember = "id";
            cboCidade.DisplayMember = "cidade";
            this.bsTelefoneUtil.DataSource = this.dtTelefoneUtil;
            this.bindingNavigator1.BindingSource = this.bsTelefoneUtil;
            this.dataGridView1.DataSource = this.bsTelefoneUtil;
            this.HabilitaBotoes(true);
        }

    }
}